package com.ruoyi.demo.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.demo.domain.DemoProductContact;
import com.ruoyi.demo.domain.DemoProductImage;
import com.ruoyi.demo.mapper.DemoProductMapper;
import com.ruoyi.demo.domain.DemoProduct;
import com.ruoyi.demo.service.IDemoProductService;

/**
 * 商品信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-10-14
 */
@Service
public class DemoProductServiceImpl implements IDemoProductService 
{
    @Autowired
    private DemoProductMapper demoProductMapper;

    /**
     * 查询商品信息
     * 
     * @param productId 商品信息主键
     * @return 商品信息
     */
    @Override
    public DemoProduct selectDemoProductByProductId(Long productId)
    {
        return demoProductMapper.selectDemoProductByProductId(productId);
    }

    /**
     * 查询商品信息列表
     * 
     * @param demoProduct 商品信息
     * @return 商品信息
     */
    @Override
    public List<DemoProduct> selectDemoProductList(DemoProduct demoProduct)
    {
        return demoProductMapper.selectDemoProductList(demoProduct);
    }

    /**
     * 新增商品信息
     *
     * @param demoProduct 商品信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertDemoProduct(DemoProduct demoProduct)
    {
        demoProduct.setCreateTime(DateUtils.getNowDate());
        demoProduct.setDelFlag("0"); // 设置删除标志为0（未删除）
        int rows = demoProductMapper.insertDemoProduct(demoProduct);
        insertDemoProductContact(demoProduct);
        insertDemoProductImage(demoProduct);
        return rows;
    }

    /**
     * 修改商品信息
     * 
     * @param demoProduct 商品信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateDemoProduct(DemoProduct demoProduct)
    {
        demoProduct.setUpdateTime(DateUtils.getNowDate());
        demoProductMapper.deleteDemoProductContactByProductId(demoProduct.getProductId());
        insertDemoProductContact(demoProduct);
        demoProductMapper.deleteDemoProductImageByProductId(demoProduct.getProductId());
        insertDemoProductImage(demoProduct);
        return demoProductMapper.updateDemoProduct(demoProduct);
    }

    /**
     * 批量删除商品信息
     * 
     * @param productIds 需要删除的商品信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDemoProductByProductIds(Long[] productIds)
    {
        demoProductMapper.deleteDemoProductContactByProductIds(productIds);
        demoProductMapper.deleteDemoProductImageByProductIds(productIds);
        return demoProductMapper.deleteDemoProductByProductIds(productIds);
    }

    /**
     * 删除商品信息信息
     * 
     * @param productId 商品信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDemoProductByProductId(Long productId)
    {
        demoProductMapper.deleteDemoProductContactByProductId(productId);
        demoProductMapper.deleteDemoProductImageByProductId(productId);
        return demoProductMapper.deleteDemoProductByProductId(productId);
    }

    /**
     * 新增商品联系方式信息
     * 
     * @param demoProduct 商品信息对象
     */
    public void insertDemoProductContact(DemoProduct demoProduct)
    {
        List<DemoProductContact> demoProductContactList = demoProduct.getContactList();
        Long productId = demoProduct.getProductId();
        if (StringUtils.isNotNull(demoProductContactList))
        {
            List<DemoProductContact> list = new ArrayList<DemoProductContact>();
            for (DemoProductContact demoProductContact : demoProductContactList)
            {
                demoProductContact.setProductId(productId);
                list.add(demoProductContact);
            }
            if (list.size() > 0)
            {
                demoProductMapper.batchDemoProductContact(list);
            }
        }
    }

    /**
     * 新增商品图片信息
     * 
     * @param demoProduct 商品信息对象
     */
    public void insertDemoProductImage(DemoProduct demoProduct)
    {
        List<DemoProductImage> demoProductImageList = demoProduct.getImageList();
        Long productId = demoProduct.getProductId();
        if (StringUtils.isNotNull(demoProductImageList))
        {
            List<DemoProductImage> list = new ArrayList<DemoProductImage>();
            for (DemoProductImage demoProductImage : demoProductImageList)
            {
                demoProductImage.setProductId(productId);
                list.add(demoProductImage);
            }
            if (list.size() > 0)
            {
                demoProductMapper.batchDemoProductImage(list);
            }
        }
    }
}

